function compose(...arr){
    return arr.reduce((a,b)=>(arg)=>a(b(arg)))
}

function applyMiddleware(...middlewares){
    return (createStore) => (reducer) =>{
        const store = createStore(reducer)
        let newDispatch = ()=>{}
        const middlewareApi ={
            dispatch:  (action, ...args) => newDispatch(action, ...args),
            getState: store.getState
        }
       const chain = middlewares.map(middleware=> middleware(middlewareApi))

       newDispatch = compose(...chain)(store.dispatch)

        return {
            ...store,
            dispatch: newDispatch
        }
    }

}
export default applyMiddleware